import {
  baseInfoTable,
  otherInfoTable,
  specialInfoTable,
} from '@v/one-map/left-community-info/build-edit-config'
import { baseFileUrl } from '@/utils'

export const getBuildDetail = (
  data,
  buildCode,
  buildId,
  isPoint = false,
  roleLevel
) => {
  data = data || {}
  const topicBuildFileVo = data.topicBuildFileVo || {}
  const topicNewBuildInfo = data.topicNewBuildInfo || {}
  let topicNewBuildRel = data.topicNewBuildRel || {}

  if (isPoint) {
    topicNewBuildRel = data.topicNewPointInfo || {}
  }

  const detail = [
    {
      question: '所属社区:',
      answer: topicNewBuildRel.communityName || '',
      unit: '',
      id: topicNewBuildRel.communityId || '',
    },
    {
      question: '所属小区:',
      answer: topicNewBuildRel.residenceName || '',
      unit: '|special-residence|',
      id: topicNewBuildRel.residenceId || '',
    },
  ]
  if (!isPoint) {
    if (roleLevel === 0 || roleLevel === 4 || roleLevel === 2) {
      detail.push({
        question: '楼宇基础信息',
        answer: '',
        unit: '|special-title|',
      })
      baseInfoTable.forEach((item) => {
        const obj = {
          question: item.label + ':',
          answer: topicNewBuildInfo[item.prop] || '',
          unit: '',
        }
        if (item.type === 'image') {
          const arr = topicBuildFileVo[item.prop] || []
          const imgArr = arr
            .filter((file) => file.filePath)
            .map((item) => {
              return baseFileUrl + item.filePath
            })
          obj.answer = imgArr.toString()
          obj.unit = '|special-file|'
        } else {
          obj.answer = topicNewBuildInfo[item.prop] || ''
        }
        if (item.prop === 'buildCode' && !obj.answer) {
          obj.answer = topicNewBuildInfo.buildCode || buildCode
        }
        detail.push(obj)
      })
    }
    detail.push({
      question: '楼宇核实',
      answer: '',
      unit: '|special-title|',
      id: topicNewBuildInfo.buildCode || buildCode,
    })
    otherInfoTable.forEach((item) => {
      const obj = {
        question: item.label + ':',
        answer: '',
        unit: '',
      }
      if (item.type === 'image') {
        const arr = topicBuildFileVo[item.prop] || []
        const imgArr = arr
          .filter((file) => file.filePath)
          .map((item) => {
            return baseFileUrl + item.filePath
          })
        obj.answer = imgArr.toString()
        obj.unit = '|special-file|'
      } else {
        obj.answer = topicNewBuildRel[item.prop] || ''
      }
      detail.push(obj)
    })
  }

  const buildSpecialInfo = []
  if (isPoint) {
    buildSpecialInfo.push({
      question: '隐患信息',
      answer: '',
      unit: '|special-title|',
      id: buildCode,
    })
  } else {
    buildSpecialInfo.push({
      question: '楼宇隐患信息',
      answer: '',
      unit: '|special-title|',
      id: buildId,
    })
  }

  specialInfoTable.forEach((item) => {
    const obj = {
      question: item.title + ':',
      answer: '',
      unit: '',
    }
    buildSpecialInfo.push(obj)
    item.questions.forEach((question) => {
      let answer = ''
      let unit = ''
      if (question.type === 'radio') {
        answer = topicNewBuildRel[question.prop] === 1 ? '是' : '否'
      } else if (question.type === 'image') {
        const arr = topicBuildFileVo[question.prop] || []
        const imgArr = arr
          .filter((file) => file.filePath)
          .map((item) => {
            return baseFileUrl + item.filePath
          })
        answer = imgArr.toString()
        unit = '|special-file|'
      } else {
        answer = topicNewBuildRel[question.prop] || ''
      }
      const questionObj = {
        question: question.label + ':',
        answer,
        unit,
      }
      buildSpecialInfo.push(questionObj)
    })
  })
  detail.push(...buildSpecialInfo)
  return detail
}
